import click
import uvicorn
from fastapi import FastAPI, Request
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates

app = FastAPI(title="caiji project", openapi_url=f"/api/v1/openapi.json")
app.mount("/static", StaticFiles(directory="static/docs"), name="static")

templates = Jinja2Templates(directory="templates")


@app.get("/static", response_class=HTMLResponse)
async def read_item(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


@click.command()
@click.option('--port', default=8001, help='set port')
def start(port):
    """
    :param port: 传入端口号，默认为8001
    :return:
    """
    # ml = "uvicorn main:app --reload --host=0.0.0.0 --port={}".format(port)
    # os.system(ml)
    uvicorn.run(app='main:app', host='0.0.0.0', port=port, reload=True)


if __name__ == '__main__':
    start()

# 启动命令
# (端口号，默认不输入参数为8000) (tags,默认为main,测试需更改为test，正常使用为main)   (app_name，默认fastapi-cjserver)
# python main.py --port 8001 --tags main --app_name fastapi-cjserver



#  python main.py --port 8001